﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraGrid.Repository;

namespace RepositoryLookupItem
{
    public partial class Form1 : DevExpress.XtraEditors.XtraForm
    {
        public Form1()
        {
            InitializeComponent();
        }

        public class Kieukhachhang01
        {
            public int? ProductID { get; set; }
            public string ProductName { get; set; }
            public int? CategoryID { get; set; }
            public short? UnitsInStock { get; set; }
        }


        NorthwindEntities nw = new NorthwindEntities();
        public void LoadData()
        {
            var x = from i in nw.Products
                    join j in nw.Categories on i.Categories.CategoryID equals j.CategoryID
                    select new Kieukhachhang01
                    {
                        ProductID = i.ProductID,
                        ProductName = i.ProductName,
                        CategoryID = j.CategoryID,
                        UnitsInStock = i.UnitsInStock
                    }; 

            //var x  = nw.Products.Select(i => i);
            //var y = nw.Categories.Select(i => i);

            //var y = from i in nw.Products
            //        join j in nw.Categories on i.Categories.CategoryID equals j.CategoryID
            //        where i.ProductID == 1
            //        select j;
            //select new
            //{
            //    i.ProductID,
            //    i.ProductName,
            //    j.CategoryID,
            //    i.UnitsInStock
            //};


            //repositoryItemLookUpEdit1.DataSource = y;
            //repositoryItemLookUpEdit1.DisplayMember = "CategoryName";
            //repositoryItemLookUpEdit1.ValueMember = "CategoryID";
            gridControl1.DataSource = x;
            //gridView1.OptionsBehavior.Editable = true;
        }

        private void simpleButton1_Click(object sender, EventArgs e)
        {
            LoadData();
            //gridView1.DataController.AllowIEnumerableDetails = true;
            var y = nw.Categories.Select(i => i);
            //repositoryItemLookUpEdit1 = new DevExpress.XtraEditors.Repository.RepositoryItemLookUpEdit();
            //repositoryItemLookUpEdit1.BeginInit();
            repositoryItemLookUpEdit1.DataSource = y;
            repositoryItemLookUpEdit1.DisplayMember = "CategoryName";
            repositoryItemLookUpEdit1.ValueMember = "CategoryID";
            //gridControl1.RepositoryItems.Add(repositoryItemLookUpEdit1);
            //gridView1.Columns["CategoryID"].ColumnEdit = repositoryItemLookUpEdit1;


        }

        private void gridView1_Click(object sender, EventArgs e)
        {
            //MessageBox.Show("oke");
        }

        private void simpleButton2_Click(object sender, EventArgs e)
        {

            //int str = Convert.ToInt32(gridView1.GetRowCellValue(gridView1.FocusedRowHandle,gridView1.Columns["ProductID"]) + string.Empty);

            ////var y = from i in nw.Products
            ////        join j in nw.Categories on i.Categories.CategoryID equals j.CategoryID
            ////        where i.ProductID == str
            ////        select j;
            //gridView1.DataController.AllowIEnumerableDetails = true;
            //var y = nw.Categories.Select(i => i);
            //repositoryItemLookUpEdit1 = new DevExpress.XtraEditors.Repository.RepositoryItemLookUpEdit();
            //repositoryItemLookUpEdit1.BeginInit();
            //repositoryItemLookUpEdit1.DataSource = y.ToList();
            //repositoryItemLookUpEdit1.DisplayMember = "CategoryName";
            //repositoryItemLookUpEdit1.ValueMember = "CategoryID";
            //gridControl1.RepositoryItems.Add(repositoryItemLookUpEdit1);
            //gridView1.Columns["CategoryID"].ColumnEdit = repositoryItemLookUpEdit1;
            ////BarEditItem = 2;


        }

        private void Form1_Load(object sender, EventArgs e)
        {
            //var y = from i in nw.Products
            //        join j in nw.Categories on i.Categories.CategoryID equals j.CategoryID
            //        where i.ProductID == 1
            //        select j.CategoryID;
            //repositoryItemLookUpEdit1.DataSource = y;
            //repositoryItemLookUpEdit1.DisplayMember = "CategoryName";
            //repositoryItemLookUpEdit1.ValueMember = "CategoryID";
            //lookUpEdit1.EditValue = y;


        }

        private void gridControl1_ViewRegistered(object sender, DevExpress.XtraGrid.ViewOperationEventArgs e)
        {
            ////gridView1.Columns["CategoryID"].ColumnEdit = repositoryItemLookUpEdit1;
            //gridView1.DataController.AllowIEnumerableDetails = true;
            //var y = nw.Categories.Select(i => i);
            //repositoryItemLookUpEdit1 = new DevExpress.XtraEditors.Repository.RepositoryItemLookUpEdit();
            //repositoryItemLookUpEdit1.BeginInit();
            //repositoryItemLookUpEdit1.DataSource = y.ToList();
            //repositoryItemLookUpEdit1.DisplayMember = "CategoryName";
            //repositoryItemLookUpEdit1.ValueMember = "CategoryID";
            //gridControl1.RepositoryItems.Add(repositoryItemLookUpEdit1);
            //gridView1.Columns["CategoryID"].ColumnEdit = repositoryItemLookUpEdit1;
     {
                
     } 
        }

        private void gridView1_ValidateRow(object sender, DevExpress.XtraGrid.Views.Base.ValidateRowEventArgs e)
        {
            if (!gridView1.IsNewItemRow(e.RowHandle))
            {
                //nw.SaveChanges();
                //MessageBox.Show("Old line");
                int str = Convert.ToInt32(gridView1.GetRowCellValue(e.RowHandle,gridView1.Columns["ProductID"]));
                //MessageBox.Show(str);


                

                var kh = nw.Products.FirstOrDefault(q => q.ProductID == str);
                kh.ProductID = Convert.ToInt32(gridView1.GetRowCellValue(e.RowHandle, gridView1.Columns["ProductID"]) + string.Empty);
                kh.ProductName = gridView1.GetRowCellValue(e.RowHandle,gridView1.Columns["ProductName"]) + string.Empty;
                kh.Categories.CategoryID = Convert.ToInt32(gridView1.GetRowCellValue(e.RowHandle, gridView1.Columns["CategoryID"]));
                kh.UnitsInStock = Convert.ToInt16(gridView1.GetRowCellValue(e.RowHandle, gridView1.Columns["UnitsInStock"]));
                nw.SaveChanges();



               // foreach(var j in array[i])
               // {
                //    MessageBox.Show(j);
                //}
                //MessageBox.Show(i.ToString());
                //MessageBox.Show(array[i].ToString());
            }



        }

        private void gridControl2_Click(object sender, EventArgs e)
        {

        }

        private void simpleButton3_Click(object sender, EventArgs e)
        {
            gridView1.AddNewRow();
        }
    }
}
